package com.regulareport.form.service.impl;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSONArray;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.regulareport.common.core.domain.entity.ReportCellDefinition;
import com.regulareport.common.core.domain.entity.ReportFormsEdit;
import com.regulareport.common.core.domain.entity.ReportTemplateProgramDefinition;
import com.regulareport.common.core.redis.RedisCache;
import com.regulareport.common.utils.StringUtils;
import com.regulareport.common.utils.ip.AddressUtils;
import com.regulareport.common.utils.poi.ExcelToJsonConverter;
import com.regulareport.form.mapper.ReportCellDefinitionMapper;
import com.regulareport.form.mapper.ReportFormsEditMapper;
import com.regulareport.form.mapper.ReportTemplateProgramDefinitionMapper;
import com.regulareport.form.service.IReportCellDefinitionService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 单元格定义Service业务层处理
 * 
 * @author
 * @date 2025-09-03
 */
@Service
public class ReportCellDefinitionServiceImpl implements IReportCellDefinitionService
{
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ReportCellDefinitionMapper reportCellDefinitionMapper;

    @Autowired
    private ReportFormsEditMapper reportFormsEditMapper;

    @Autowired
    private ReportTemplateProgramDefinitionMapper reportTemplateProgramDefinitionMapper;

    @Autowired
    private RedisCache redisCache;

    @Value("${token.expireTime}")
    private int expireTime;

    /**
     * 查询单元格定义
     * 
     * @param reportFormID 银监模板ID
     * @param cellAddress 表格所在行号
     * @return 单元格定义
     */
    @Override
    public ReportCellDefinition selectReportCellDefinitionByCompositeKeys(String reportFormID, String cellAddress) throws Exception
    {
//        List<Map> addressList = ExcelToJsonConverter.getCellsAddress(cellAddress);
//        Map minMapIndex = (Map) addressList.get(0);
//        if(addressList.size()>1){
//            Map maxMapIndex = (Map) addressList.get(addressList.size()-1);
//        }
        if(StringUtils.isEmpty(cellAddress)){
            throw new Exception("未选中单元格");
        }
        String[] addrArr =  cellAddress.split(":");
        if(addrArr.length > 1){
            cellAddress = addrArr[0];
        }
        CellReference ref = new CellReference(cellAddress);
        ReportCellDefinition resultReportCellDef = reportCellDefinitionMapper.selectReportCellDefinitionByCompositeKeys(reportFormID, ref.getRow(), ref.getCol());
        if(resultReportCellDef == null || resultReportCellDef.equals("")){
            throw new Exception("请先设置单元格属性");
        }
        return resultReportCellDef;
    }

    /**
     * 查询单元格定义列表
     * 
     * @param reportCellDefinition 单元格定义
     * @return 单元格定义
     */
    @Override
    public List<ReportCellDefinition> selectReportCellDefinitionList(ReportCellDefinition reportCellDefinition)
    {
        return reportCellDefinitionMapper.selectReportCellDefinitionList(reportCellDefinition);
    }

    /**
     * 新增单元格定义
     * 
     * @param reportCellDefinition 单元格定义
     * @return 结果
     */
    @Override
    public String insertReportCellDefinition(ReportCellDefinition reportCellDefinition) throws Exception {

        //目前还未考虑情况  左列编号名称 可能有全行合并情况
        ReportFormsEdit reportFormsEdit = reportFormsEditMapper.selectReportFormsEditByPkId(reportCellDefinition.getReportFormID());
        ObjectMapper mapper = new ObjectMapper();
        String templateStr = "";
        //单元格定义转化
        try {
            byte[] templateByteArr = redisCache.getCacheObject(String.valueOf((reportFormsEdit.getReportFormNo() + "_" + reportFormsEdit.getEdition()).getBytes(StandardCharsets.UTF_8)));
            if(templateByteArr != null && templateByteArr.length>0) {
                templateStr = new String(templateByteArr, StandardCharsets.UTF_8);
            }
        } catch (Exception e) {
            throw new RuntimeException("字符编码转换失败", e);
        }
        //判断左列  和 上行


        //如果缓存不存在从数据库查模板信息
        if(StringUtils.isEmpty(templateStr)){
            //暂时用前台传过来的报表ID
            byte[] fileJsonArr = reportFormsEdit.getFileJson();
//            templateStr = ExcelToJsonConverter.convertByteArrayWithInputStream(fileJsonArr);
            InputStream input = new ByteArrayInputStream(fileJsonArr);
            templateStr = ExcelToJsonConverter.convertExcelWithStyles(input);
            redisCache.setCacheObject( reportFormsEdit.getReportFormNo() + "_" + reportFormsEdit.getEdition(), templateStr, expireTime, TimeUnit.DAYS);
        }
        //String转Map
        Map templateMap = mapper.readValue(templateStr, new TypeReference<Map<String, Object>>(){});
        Map sheet1Map = (Map) templateMap.get("sheet1");
        Map rowsMap = (Map)sheet1Map.get("rows");
        //待定义的单元格   size最小为1
        List mergeCellAddrArr = ExcelToJsonConverter.getCellsAddress(reportCellDefinition.getCellRow());
        //左侧列 编号和说明  size最小为1
        List indexCellAddr =  ExcelToJsonConverter.getCellsAddress(reportCellDefinition.getIndexDesFirCol());
        //表格上侧说明单元行 size最小为1
        List desRowAddrArr = ExcelToJsonConverter.getCellsAddress(reportCellDefinition.getIndexDesFirRow());
        //表格上侧编号单元行 size最小为1
        List identifierRowAddrArr = ExcelToJsonConverter.getCellsAddress(reportCellDefinition.getIndexNumFirRow());
        if(CollectionUtils.isEmpty(mergeCellAddrArr)){
            throw new Exception("未收到选中的单元格");
        }
        if(CollectionUtils.isEmpty(desRowAddrArr)){
            throw new Exception("未收到名称命名行信息");
        }
        if(CollectionUtils.isEmpty(identifierRowAddrArr)){
            throw new Exception("未收到编号行信息");
        }
        if(CollectionUtils.isEmpty(indexCellAddr)){
            throw new Exception("未收到编号和名称列信息");
        }
        //判断选中表格范围和输入参数是否正常
        compareCoordinate(mergeCellAddrArr, indexCellAddr, desRowAddrArr, identifierRowAddrArr);

        //左列的编号和名称
        Map<String, String> indexCellNameMap = new HashMap();
        Map<String, String> indexNameTemplateMap = new HashMap();
        //左列的编号和名称
        ObjectNode indexCellNameNode = objectMapper.createObjectNode();
        //表头的名称行
        ObjectNode desRowNode = objectMapper.createObjectNode();
        //表头的编号行
        ObjectNode identifierNode = objectMapper.createObjectNode();

            //组装 左列的编号和名称  ---- GF1900 编号的模板需要单独处理
        Map minMapIndex = (Map) indexCellAddr.get(0);
        List indexCellNamelist ;
           //如果只有一个数据这里会下标越界
            Map maxMapIndex = (Map) indexCellAddr.get(indexCellAddr.size()-1);
            for(int i = (Integer)minMapIndex.get("row"); i <= (Integer)maxMapIndex.get("row"); i++){
                Map rowObject = (Map) rowsMap.get(String.valueOf(i));
                Map colsMap = (Map) rowObject.get("cells");
                StringBuilder indexCellName = new StringBuilder();
                boolean isMergeAdd = true; //当合并超过5列时，当前行将不再作为映射基础
                for(int j = (Integer)minMapIndex.get("col"); j <= (Integer)maxMapIndex.get("col"); j++){
                    Map cellMap = (Map) colsMap.get(String.valueOf(j));
                    if(cellMap != null && cellMap.get("isMerged") != null && (boolean)cellMap.get("isMerged")) {
                        //当前字符串不包含待append的字符串时，append,解决列合并场景
                        if(!indexCellName.toString().contains(((String)cellMap.get("mergeText1")).trim())) {
                            indexCellName = StringUtils.isEmpty(indexCellName)?indexCellName.append(((String) cellMap.get("mergeText1")).trim()):indexCellName.append("_").append(((String) cellMap.get("mergeText1")).trim());
                        }
                        if(cellMap.get("merge") != null) {
                            List mergeList = (List) cellMap.get("merge");
                            System.out.println(" reportFormNO========"+reportCellDefinition.getReportFormNo() + ", mergeArr==" + mergeList.get(1) + ", i== " +i+ ",  j==" +j);
                            if(((Integer)mergeList.get(1)) > 0){//大于0，等于同行合并2列以上  merge该值为合并的最后一列减合并的开始列
                               isMergeAdd = false;
                            }
                        } else if(cellMap.get("merge") == null && j > 0) { //合并整行 包括把第一列序号也合并，获取前一列的 j大于0，当超过一整行合并时可能会出现合并行依然保存映射表情况
                            Map cellMapMerge = (Map) colsMap.get(String.valueOf(j-1));
                            if(cellMapMerge.get("merge") != null) {
                                List mergeList = (List) cellMapMerge.get("merge");
                                System.out.println(" reportFormNO========"+reportCellDefinition.getReportFormNo() + ", mergeArr==" + mergeList.get(1) + ", i== " +i+ ",  j==" +j);
                                if(((Integer)mergeList.get(1)) > 0){
                                   isMergeAdd = false;
                                }
                            }
                        }
                        //因为此处是名称列，所以必然不是有边框表格全部，所以下一个表格也在边框表格之内
                        Map judgeNextCell = (Map) colsMap.get(String.valueOf(j+1)); //名称栏不合并，但是指标栏合并 SF6301 涉及
                        if(judgeNextCell != null && judgeNextCell.get("isMerged") != null && !(boolean)judgeNextCell.get("isMerged")){
                            isMergeAdd = true;
                        }
                    } else {
                        //暂未考虑非末列是合并单元格的情况
                        indexCellName = StringUtils.isEmpty(indexCellName)?indexCellName.append(((String)cellMap.get("text")).trim()):indexCellName.append("_").append(((String)cellMap.get("text")).trim());
                        Map judgeCell = (Map) colsMap.get(String.valueOf(j+1)); //名称栏不合并，但是指标栏合并 SF6301 涉及
                        if(judgeCell != null && judgeCell.get("isMerged") != null && (boolean)judgeCell.get("isMerged")){
                            isMergeAdd = false;
                        }
                    }
                }

                // GF1900 解析 汉字+数字 为 数字+汉字
                if(reportFormsEdit != null && "GF1900".equals(reportFormsEdit.getReportFormNo()) && StringUtils.isNotEmpty(indexCellName)){
                    //GF1900的最后一行，未输入罗马数字时，默认为Ⅰ
                    String indexDesSecCol = StringUtils.isEmpty(reportCellDefinition.getIndexDesSecCol()) ? "Ⅰ" : reportCellDefinition.getIndexDesSecCol();
                    indexCellName = new StringBuilder(ExcelToJsonConverter.swapTextAndNumber(String.valueOf(indexCellName), indexDesSecCol));
                }
                indexCellNamelist = ExcelToJsonConverter.parseStrings(new String[]{String.valueOf(indexCellName)});
                //key 为行数 row, value 为表格值的转化对象（Number + name）
                indexCellNameNode.set(String.valueOf(i), (JsonNode) indexCellNamelist.get(0));
                if(isMergeAdd) {
                    indexCellNameMap.put(String.valueOf(i), String.valueOf(indexCellName).replaceAll("\r\n|\n", "").replaceAll(" ", ""));
                }
            }

        //组装 表的名称行 先遍历列 再遍历行
        Map minDesRowMap = (Map) desRowAddrArr.get(0);
            Map maxDesRowMap = (Map) desRowAddrArr.get(desRowAddrArr.size()-1);
            for(int j = (Integer)minDesRowMap.get("col"); j <= (Integer)maxDesRowMap.get("col"); j++){
                StringBuilder indexCellName = new StringBuilder();
                for(int i = (Integer)minDesRowMap.get("row"); i <= (Integer)maxDesRowMap.get("row"); i++){
                    Map rowObject = (Map) rowsMap.get(String.valueOf(i));
                    Map colsMap = (Map) rowObject.get("cells");
                    Map cellMap = (Map) colsMap.get(String.valueOf(j));
                    //首行合并不判断，可能涉及列合并情况未考虑
                    if((Integer)minDesRowMap.get("row") < i && cellMap.get("isMerged") != null && (boolean)cellMap.get("isMerged")){
                        //表头指标名称 判断上一行该列值是否和当前值相等(不存在 溢出 -1 行)
                        Map mergeColsMap = (Map) rowsMap.get(String.valueOf(i-1));
                        Map mergeColMap = (Map) mergeColsMap.get("cells");
                        Map mergeCellMap = (Map) mergeColMap.get(String.valueOf(j));
                        if(!mergeCellMap.get("mergeText1").equals(cellMap.get("mergeText1"))) {
                            indexCellName = indexCellName.append(((String)cellMap.get("mergeText1")).trim());
                        }
                    } else {
                        if (cellMap.get("text") != null) {
                            //暂未考虑非末列是合并单元格的情况
                            indexCellName = indexCellName.append(((String)cellMap.get("text")).trim());
                        }else{
                            indexCellName = indexCellName.append(((String)cellMap.get("mergeText1")).trim());
                        }
                    }
                    desRowNode.put(String.valueOf(j), String.valueOf(indexCellName));
                }
            }

        //组装 表的编号行
        Map minIdentifierMap = (Map) identifierRowAddrArr.get(0);
//        if(identifierRowAddrArr.size() > 1 ){
            Map maxIdentifierMap = (Map) identifierRowAddrArr.get(identifierRowAddrArr.size()-1);
            for(int i = (Integer)minIdentifierMap.get("row"); i <= (Integer)maxIdentifierMap.get("row"); i++){
                Map rowObject = (Map) rowsMap.get(String.valueOf(i));
                Map colsMap = (Map) rowObject.get("cells");
                for(int j = (Integer)minIdentifierMap.get("col"); j <= (Integer)maxIdentifierMap.get("col"); j++){
                    Map cellMap = (Map) colsMap.get(String.valueOf(j));
                    identifierNode.put(String.valueOf(j), ((String)cellMap.get("text")).trim());
                }
            }
//        } else {
//            String row = (String)minIdentifierMap.get("row");
//            String col = (String)minIdentifierMap.get("col");
//            Map rowObject = (Map) rowsMap.get(row);
//            Map colsMap = (Map) rowObject.get("cells");
//            Map cellMap = (Map) colsMap.get(col);
//            String cellText = ((String) cellMap.get("text")).trim();
//            identifierNode.put(col, cellText);
//        }

        List reportCellDefList = new ArrayList();
        Map minMapAddr = (Map) mergeCellAddrArr.get(0);
            Map maxMapAddr = (Map) mergeCellAddrArr.get(mergeCellAddrArr.size() - 1);
            for (int i = (Integer) minMapAddr.get("row"); i <= (Integer) maxMapAddr.get("row"); i++) {
                Map rowObject = (Map) rowsMap.get(String.valueOf(i));
                Map colsMap = (Map) rowObject.get("cells");
                for (int j = (Integer) minMapAddr.get("col"); j <= (Integer) maxMapAddr.get("col"); j++) {
                    Map cellMap = (Map) colsMap.get(String.valueOf(j));
                    //模板背景色为灰  不生成定义
                    if (cellMap!=null && cellMap.get("bgColor") != null && cellMap.get("bgColor").equals("#d0cecf")) {
                        continue;
                    }
                    //合并单元格不生成定义
                    if (cellMap!=null && cellMap.get("isMerged") != null && (boolean)cellMap.get("isMerged")) {
                        continue;
                    }
                    boolean editable = (boolean) cellMap.get("editable");
                    //获取公式
                    String formula = cellMap.get("formula") != null ? (String) cellMap.get("formula") : "";
                    ReportCellDefinition reportCellDefVO = new ReportCellDefinition();
                    reportCellDefVO.setReportFormNo(reportFormsEdit.getReportFormNo());
                    reportCellDefVO.setEdition(reportFormsEdit.getEdition().substring(0, 4));
                    reportCellDefVO.setReportFormID(reportFormsEdit.getPkId());
                    reportCellDefVO.setCellRow(String.valueOf(i));
                    reportCellDefVO.setCellCol(String.valueOf(j));
                    reportCellDefVO.setCoordinate(ExcelToJsonConverter.getCellReference(i, j));
//                可能为空字符串      String   = ((Map)indexNameList.get(i-Integer.valueOf((String)minMapAddr.get("row")))).get("number")
                    //组装指标编号
                    String indexNo = sheet1Map.get("name") + "_" + indexCellNameNode.get(String.valueOf(i)).get("number").asText() + "_" + identifierNode.get(String.valueOf(j)).asText();
                    //组装指标名称
                    String indexName = sheet1Map.get("name") + "_" + indexCellNameNode.get(String.valueOf(i)).get("name").asText() + "_" + desRowNode.get(String.valueOf(j)).asText();
                    //SF6700需要增加罗马数字 Ⅰ  Ⅱ  Ⅲ   Ⅳ    Ⅴ  Ⅵ
                    if (reportFormsEdit != null && "SF6700".equals(reportFormsEdit.getReportFormNo())) {
                        indexNo = StringUtils.isEmpty(reportCellDefinition.getIndexDesSecCol()) ? indexNo : indexNo + "_" + reportCellDefinition.getIndexDesSecCol();
                    }
                    //处理自动换行
                    indexName = indexName.replaceAll("\r\n|\n", "");
                    reportCellDefVO.setIndexNo(indexNo);
                    reportCellDefVO.setIndexName(indexName);
                    if (editable) {
                        //可编辑
                        reportCellDefVO.setEditable(true);
                    } else {
                        //不可编辑
                        reportCellDefVO.setEditable(false);
                    }
                    if (StringUtils.isNotEmpty(formula)) {
                        reportCellDefVO.setFormula(formula);
                    }
                    reportCellDefVO.setIndexNumFirCol(identifierNode.get(String.valueOf(j)).asText());
                    reportCellDefVO.setIndexDesFirCol(desRowNode.get(String.valueOf(j)).asText());
                    reportCellDefVO.setIndexDesFirRow(indexCellNameNode.get(String.valueOf(i)).get("name").asText());
                    reportCellDefVO.setIndexNumFirRow(indexCellNameNode.get(String.valueOf(i)).get("number").asText());
                    reportCellDefVO.setCreateBy(reportCellDefinition.getCreateBy());
                    reportCellDefList.add(reportCellDefVO);
                }
            }

            List<ReportTemplateProgramDefinition> reTePrDeList = new ArrayList<>();

            for (Map.Entry<String, String> entry : indexCellNameMap.entrySet()) {
                ReportTemplateProgramDefinition definition = new ReportTemplateProgramDefinition();
                definition.setCoordinate(Integer.valueOf(entry.getKey()));
                definition.setProgramName(entry.getValue().replaceAll(" ", "")); //去掉空格
                definition.setReportFormNo(reportFormsEdit.getReportFormNo());
                definition.setReportFormID(reportFormsEdit.getPkId());
                definition.setEdition(reportFormsEdit.getEdition());
                definition.setCreateBy(reportCellDefinition.getCreateBy());
                reTePrDeList.add(definition);
            }

            //保存银监模板中与总行报表映射的信息项
            reportTemplateProgramDefinitionMapper.batchInsertReportTemplateProgramDefinition(reTePrDeList);

            //计算前端选中的表格范围总共有多少个格子
            CellRangeAddress range = CellRangeAddress.valueOf(reportCellDefinition.getCellRow());
            int rows = range.getLastRow() - range.getFirstRow() + 1;
            int cols = range.getLastColumn() - range.getFirstColumn() + 1;
            int totalCells = rows * cols;
//            int saveInt = 0;

            int saveInt = reportCellDefinitionMapper.batchInsertReportCellDefinition(reportCellDefList);

            return "选中范围内总计： " + totalCells + "格， 成功保存： " + saveInt +  "格";
    }

    /**
     * 修改单元格定义
     * 
     * @param reportCellDefinition 单元格定义
     * @return 结果
     */
    @Override
    public int updateReportCellDefinition(ReportCellDefinition reportCellDefinition) throws Exception
    {
        List<Map> coordinateList = ExcelToJsonConverter.getCellsAddress(reportCellDefinition.getCellRow());
        if(CollectionUtils.isEmpty(coordinateList)){
            throw new Exception("未收到选中的表格范围");
        }
        List<ReportCellDefinition> reportCellDefinitionList = new ArrayList<>();
        //只选择一个格子时  选择多格 首个cell
        Map minMapIndex = coordinateList.get(0);
        if(coordinateList.size()>1){
            Map maxMapIndex = coordinateList.get(coordinateList.size()-1);
            for(int i = (Integer)minMapIndex.get("row"); i <= (Integer)maxMapIndex.get("row"); i++) {
                for(int j = (Integer)minMapIndex.get("col"); j <= (Integer)maxMapIndex.get("col"); j++) {
                    ReportCellDefinition reportCellDef = new ReportCellDefinition();
                    reportCellDef.setReportFormID(reportCellDefinition.getReportFormID());
                    reportCellDef.setCellRow(String.valueOf(i));
                    reportCellDef.setCellCol(String.valueOf(j));
                    reportCellDef.setCurrency(reportCellDefinition.getCurrency());
                    reportCellDef.setDecimalPlaces(reportCellDefinition.getDecimalPlaces());
                    reportCellDef.setMonetaryUnit(reportCellDefinition.getMonetaryUnit());
                    reportCellDef.setTolerance(reportCellDefinition.getTolerance());
                    reportCellDef.setEditable(reportCellDefinition.isEditable());
                    reportCellDef.setDataType(reportCellDefinition.getDataType());
                    reportCellDefinitionList.add(reportCellDef);
                }
            }
            return reportCellDefinitionMapper.updateBatchReportCellDefinition(reportCellDefinitionList);
        } else {
            reportCellDefinition.setCellRow(String.valueOf(minMapIndex.get("row")));
            reportCellDefinition.setCellCol(String.valueOf(minMapIndex.get("col")));
            return reportCellDefinitionMapper.updateReportCellDefinition(reportCellDefinition);
        }
    }

    @Override
    public List<ReportCellDefinition> selectReportCellDefinitionByReportFormName(String reportFormName)
    {
        return reportCellDefinitionMapper.selectReportCellDefinitionByReportFormName(reportFormName);
    }

    private void compareCoordinate(List mergeCellAddrArr, List indexCellAddr, List desRowAddrArr, List identifierRowAddrArr) throws Exception {
        Map minMrgeCellAddr = (Map) mergeCellAddrArr.get(0);
        Map minDesRowAddr = (Map) desRowAddrArr.get(0);
        Map minIdentifierRowAddr = (Map) identifierRowAddrArr.get(0);
        Map minIndexCellAddr = (Map) indexCellAddr.get(0);
        Map maxMrgeCellAddr = (Map) mergeCellAddrArr.get(mergeCellAddrArr.size()-1);
        Map maxDesRowAddr = (Map) desRowAddrArr.get(desRowAddrArr.size()-1);
        Map maxIdentifierRowAddr = (Map) identifierRowAddrArr.get(identifierRowAddrArr.size()-1);
        Map maxIndexCellAddr = (Map) indexCellAddr.get(indexCellAddr.size()-1);
        int minMergeRow = Integer.valueOf(minMrgeCellAddr.get("row").toString());
        int minMergeCellCol = Integer.valueOf(minMrgeCellAddr.get("col").toString());
        int minIndexCellRow = Integer.valueOf(minIndexCellAddr.get("row").toString());
        int minDesRowCol = Integer.valueOf(minDesRowAddr.get("col").toString());
        int minIdentifierCol = Integer.valueOf(minIdentifierRowAddr.get("col").toString());
        if(minMergeRow < minIndexCellRow ){
            throw new Exception("输入的指标范围开始值大于所选单元格范围, 请重新输入指标范围") ;
        }
        if(minMergeCellCol < minDesRowCol ){
            throw new Exception("输入的指标键二开始值大于所选单元格列范围, 请重新输入指标键二");
        }
        if(minMergeCellCol < minIdentifierCol ){
            throw new Exception("输入的指标键一的开始值大于所选单元格列范围, 请重新输入指标键一");
        }

        int mrgeCellRow = Integer.valueOf(maxMrgeCellAddr.get("row").toString());
        int mrgeCellCol = Integer.valueOf(maxMrgeCellAddr.get("col").toString());
        int indexCellRow = Integer.valueOf(maxIndexCellAddr.get("row").toString());
        int desRowAddrCol = Integer.valueOf(maxDesRowAddr.get("col").toString());
        int identifierCol = Integer.valueOf(maxIdentifierRowAddr.get("col").toString());
        if(indexCellRow < mrgeCellRow ){
            throw new Exception("输入的指标范围小于所选单元格范围, 请重新输入指标范围") ;
        }
        if(desRowAddrCol < mrgeCellCol ){
            throw new Exception("输入的指标键二小于所选单元格列范围, 请重新输入指标键二");
        }
        if(identifierCol < mrgeCellCol ){
            throw new Exception("输入的指标键一小于所选单元格列范围, 请重新输入指标键一");
        }



    }

}
